package json;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class JsonTest {

    public static void main(String[] args) {
        //javaToJsonStr();
        //javaListToJson();
        //javaMapToJson();
        //jsonStrToJava();
        //jsonStrToJava1();
        //jsonToJavaList();
        jsonToMap();
    }
    /**
     *@Author: caoxiabo on 2019/12/17 11:46
     *@Description: java对象转json对象，json数组对象
     */
    private static void javaToJsonStr() {
        Students students = new Students();
        students.setStuName("张三");
        students.setStuAge("28");
        students.setAddress("广州天河区");
        // 1、使用JSONObject
        JSONObject strJson = JSONObject.fromObject(students);
        // 2、使用JSONArray
        JSONArray strArray = JSONArray.fromObject(students);

        System.out.println("Java对象转JSON字符串");
        System.out.println("JSON:" + strJson.toString());
        System.out.println("Array:" + strArray.toString());
    }
    /**
     *@Author: caoxiabo on 2019/12/17 11:52
     *@Description: java对象集合转json数组对象
     */
    private static void javaListToJson() {
        List<Students> lists = new ArrayList<Students>();
        for (int i=0;i<3;i++) {
            Students stu = new Students();
            stu.setStuName("张三"+i);
            stu.setStuAge(""+(28+i));
            stu.setAddress("广州天河区");
            lists.add(stu);
        }
        JSONArray listArray = JSONArray.fromObject(lists);
        System.out.println("listArray:" + listArray.toString());
    }
    /**
     *@Author: caoxiabo on 2019/12/17 11:55
     *@Description: map对象转json对象，json数组对象
     */
    private static void javaMapToJson() {
        Students stu = new Students();
        stu.setStuName("张三");
        stu.setStuAge("28");
        stu.setAddress("广州天河区");
        Map<String, Students> map = new HashMap<String, Students>();
        map.put("first", stu);

        // 1、JSONObject
        JSONObject mapObject = JSONObject.fromObject(map);
        System.out.println("mapObject" + mapObject.toString());

        // 2、JSONArray
        JSONArray mapArray = JSONArray.fromObject(map);
        System.out.println("mapArray:" + mapArray.toString());
    }

    private static void jsonStrToJava() {
        // 定义两种不同格式的字符串
        String objectStr = "{\"stuName\":\"张三\",\"stuAge\":\"28\",\"address\":\"广州天河区\"}";
        String arrayStr = "[{\"stuName\":\"张三\",\"stuAge\":\"28\",\"address\":\"广州天河区\"}]";

        // 1、使用JSONObject
        JSONObject jsonObject = JSONObject.fromObject(objectStr);
        Students students = (Students) JSONObject.toBean(jsonObject, Students.class);

        // 2、使用JSONArray
        JSONArray jsonArray = JSONArray.fromObject(arrayStr);
        // 获得jsonArray的第一个元素
        Object o = jsonArray.get(0);
        JSONObject jsonObject2 = JSONObject.fromObject(o);
        Students students2 = (Students) JSONObject.toBean(jsonObject2, Students.class);

        System.out.println("JSON字符串转Java对象");
        System.out.println("students:" + students);
        System.out.println("students2:" + students2);
    }
    /**
     *@Author: caoxiabo on 2019/12/17 13:34
     *@Description: 解析部分json数据，择其需要的属性解析
     */
    private static void jsonStrToJava1() {
        // 定义两种不同格式的字符串
        String objectStr = "{\"stuName\":\"张三\",\"stuAge\":\"28\",\"address\":\"广州天河区\"}";
        String arrayStr = "[{\"stuName\":\"张三\",\"stuAge\":\"28\",\"address\":\"广州天河区\"}]";

        // 1、使用JSONObject
        JSONObject jsonObject = JSONObject.fromObject(objectStr);
        StuTest students = (StuTest) JSONObject.toBean(jsonObject, StuTest.class);

        // 2、使用JSONArray
        JSONArray jsonArray = JSONArray.fromObject(arrayStr);
        // 获得jsonArray的第一个元素
        Object o = jsonArray.get(0);
        JSONObject jsonObject2 = JSONObject.fromObject(o);
        StuTest students2 = (StuTest) JSONObject.toBean(jsonObject2, StuTest.class);

        System.out.println("JSON字符串转Java对象");
        System.out.println("students:" + students);
        System.out.println("students2:" + students2);
    }

    // Json字符串转换为List对象
    private static void jsonToJavaList() {
        String arrayStr = "[{\"stuName\":\"张三\",\"stuAge\":\"28\",\"address\":\"广州天河区\"},{\"stuName\":\"张四\",\"stuAge\":\"29\",\"address\":\"广州天河区\"}]";
        // 转化为list
        List<Students> list = (List<Students>) JSONArray.toList(JSONArray.fromObject(arrayStr), Students.class);
        for (Students svo : list) {
            System.out.println(svo);
        }

        // 转化为数组
        Students[] stuArray = (Students[]) JSONArray.toArray(JSONArray.fromObject(arrayStr), Students.class);
        for (Students student : stuArray) {
            System.out.println(student);
        }
    }

    // Json字符串转Map
    private static void jsonToMap() {
        String strObject = "{\"first\":{\"address\":\"广州天河区\",\"stuAge\":\"28\",\"stuName\":\"张三\"}}";
        // JSONObject
        JSONObject jsonObject = JSONObject.fromObject(strObject);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("first", Students.class);

        // 使用了toBean方法，需要三个参数
        MyBean my = (MyBean) JSONObject.toBean(jsonObject, MyBean.class, map);
        System.out.println(my.getFirst());
    }

    //map中多个数据
    private static Map<String, Object> JsonToMap(String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject json = JSONObject.fromObject(jsonStr);
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(JsonToMap(json2.toString()));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
}
